using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using MvcCms.Data;
using MvcCms.Service;
using Microsoft.Practices.ServiceLocation;
namespace MvcCms.Web.Code
{
    internal static class Routing
    {
        /// <summary>
        /// Sets the application routes.
        /// </summary>        
        public static void SetAppRoutes()
        {
            var routeVals = new RouteValueDictionary();
            var constraints = new RouteValueDictionary();
            //this is the route for the homepage with a blank name and cat id of 1
            routeVals.Add("controller", "home");
            routeVals.Add("action", "index");
            RouteTable.Routes.Add(new Route("", new MvcRouteHandler())
            {
                Defaults = routeVals,
                Constraints = constraints,
                DataTokens = new RouteValueDictionary { { "contentcatid", 1 } }
            });

            routeVals = new RouteValueDictionary();
            constraints = new RouteValueDictionary();
            routeVals.Add("controller", "home");
            routeVals.Add("action", "index");
            RouteTable.Routes.Add(new Route("home/{*path}", new MvcRouteHandler())
            {
                Defaults = routeVals,
                Constraints = constraints,
                DataTokens = new RouteValueDictionary { { "contentcatid", 1 } }
            });

            ICmsService service = ServiceLocator.Current.GetInstance<ICmsService>();

            
            IEnumerable<ContentCategory> categories = service.ListAllCategories();

            foreach (ContentCategory cat in categories)
            {
                if (cat.Title == "Home") continue;
                if (cat.UsesCustomAreaRoute) continue;
                routeVals = new RouteValueDictionary();
                constraints = new RouteValueDictionary();

                routeVals.Add("controller", "home");
                routeVals.Add("action", "index");

                constraints.Add("path", "[a-zA-Z0-9\\-]*");
                if (cat.ParentCategory == null)
                {
                    RouteTable.Routes.Add(new Route(cat.Path + "/{*path}", new MvcRouteHandler())
                    {
                        Defaults = routeVals,
                        Constraints = constraints,
                        DataTokens =
                            new RouteValueDictionary { { "contentcatid", cat.CategoryID } }
                    });
                }
                else
                {
                    RouteTable.Routes.Add(new Route(cat.Path + "/{*path}", new MvcRouteHandler())
                    {
                        Defaults = routeVals,
                        Constraints = constraints,
                        DataTokens =
                            new RouteValueDictionary
                              {
                                  {"contentcatid", cat.CategoryID},
                                  {"issubcatroute", "true"},
                                  {"parentlist", cat.ParentList}
                              }
                    });
                }
            }
        }
    }
}